#pragma once

#include <cstdio>
#include <cstring>
#include <cstdarg>
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <ctime>
#include <time.h>
#include <string.h>

//日志是有自己的等级的

#define DEBUG 0
#define INFO 1
#define WARNING 2
#define ERROR 3 //一般错误 不影响服务器继续运行
#define FATAL 4

//日志格式：等级 时间 pid 消息体
std::string tolevelstring(int level)
{
    switch (level)
    {
        case 0:return "DEBUG";
        case 1: return "INFO";
        case 2: return "WARNING";
        case 3: return "ERROR";
        case 4: return "FATAL";
    
    default:
        return "";
    }
}
std::string getTime()
{
    time_t curr_time=time(nullptr);
    tm* time=localtime(&curr_time);
    return "( "+std::to_string(time->tm_sec) +" "+ std::to_string(time->tm_min)+" "+std::to_string(time->tm_hour)+" )";
}
void logmessage(int level,const char* format,...)
{
    char logLeft[1024];
    std::string s=tolevelstring(level);
    std::string curr_time=getTime();
    snprintf(logLeft,sizeof(logLeft),"%s %s %d",s.c_str(),curr_time.c_str(),getpid());
    char logRight[1024];
    va_list p;
    va_start(p,format);
    vsnprintf(logRight,sizeof(logRight),format,p);
    va_end(p);
    printf("%s%s\n",logLeft,logRight);
    //把日志保存到文件中


    //预备知识
    // va_list p;//char *
    // va_start(p,format); //让p指向可变参数的起始地址
    // va_end(p); //让p置空
    // int a=va_args(p,int); //根据类型提取参数
}
